#!/bin/bash
# 版本: 1.0
# 作者: huangsheng
# 兼容系统版本:centos7
# 验证:验证

# 备份原有的 YUM 配置文件
echo "正在备份原有的 YUM 配置文件..."
cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
echo "备份完成！"

# 提示用户选择源
echo "请选择要使用的镜像源:"
echo "1) 阿里云源"
echo "2) 清华大学源"
echo "3) 网易163源"
read -p "请输入选择的源（1/2/3）: " choice

# 定义镜像源内容
aliyun_repo="[base]
name=Aliyun CentOS-\$releasever - Base
baseurl=http://mirrors.aliyun.com/centos/\$releasever/os/\$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

[updates]
name=Aliyun CentOS-\$releasever - Updates
baseurl=http://mirrors.aliyun.com/centos/\$releasever/updates/\$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

[extras]
name=Aliyun CentOS-\$releasever - Extras
baseurl=http://mirrors.aliyun.com/centos/\$releasever/extras/\$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7"

tsinghua_repo="[base]
name=CentOS-\$releasever - Base - TUNA
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/os/\$basearch/
gpgcheck=1
gpgkey=https://mirrors.tuna.tsinghua.edu.cn/centos/RPM-GPG-KEY-CentOS-7

[updates]
name=CentOS-\$releasever - Updates - TUNA
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/updates/\$basearch/
gpgcheck=1
gpgkey=https://mirrors.tuna.tsinghua.edu.cn/centos/RPM-GPG-KEY-CentOS-7

[extras]
name=CentOS-\$releasever - Extras - TUNA
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/extras/\$basearch/
gpgcheck=1
gpgkey=https://mirrors.tuna.tsinghua.edu.cn/centos/RPM-GPG-KEY-CentOS-7"

netease_repo="[base]
name=NetEase CentOS-\$releasever - Base
baseurl=http://mirrors.163.com/centos/\$releasever/os/\$basearch/
gpgcheck=1
gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

[updates]
name=NetEase CentOS-\$releasever - Updates
baseurl=http://mirrors.163.com/centos/\$releasever/updates/\$basearch/
gpgcheck=1
gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

[extras]
name=NetEase CentOS-\$releasever - Extras
baseurl=http://mirrors.163.com/centos/\$releasever/extras/\$basearch/
gpgcheck=1
gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7"

# 根据用户选择替换源
case $choice in
    1)
        echo "正在更换为阿里云源..."
        echo "$aliyun_repo" > /etc/yum.repos.d/CentOS-Base.repo
        ;;
    2)
        echo "正在更换为清华大学源..."
        echo "$tsinghua_repo" > /etc/yum.repos.d/CentOS-Base.repo
        ;;
    3)
        echo "正在更换为网易163源..."
        echo "$netease_repo" > /etc/yum.repos.d/CentOS-Base.repo
        ;;
    *)
        echo "无效的选择！"
        exit 1
        ;;
esac

# 清理缓存并更新
echo "清理 YUM 缓存并更新..."
yum clean all
yum makecache

echo "源更换完成！"
